﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Prime.Models;

namespace Prime.Controllers
{
    public class StudentCourseProgressController : ApiController
    {
        private LearningDbEntities db = new LearningDbEntities();

        // GET api/StudentCourseProgress
        public IQueryable<StudentCourseProgress> GetStudentCourseProgresses()
        {
            return db.StudentCourseProgresses;
        }

        // GET api/StudentCourseProgress/5
        [ResponseType(typeof(StudentCourseProgress))]
        public IHttpActionResult GetStudentCourseProgress(int id)
        {
            var studentcourseprogress =
                from t in db.StudentCourseProgresses
                where t.UserName == User.Identity.Name && t.CourseId == id
                select t;
            if (studentcourseprogress.Any() == false)
            {
                return NotFound();
            }

            return Ok(studentcourseprogress.First());
        }

        // PUT api/StudentCourseProgress/5
        public IHttpActionResult PutStudentCourseProgress(int id, StudentCourseProgress studentcourseprogress)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != studentcourseprogress.CourseId)
            {
                return BadRequest();
            }

            db.Entry(studentcourseprogress).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentCourseProgressExists(User.Identity.Name))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/StudentCourseProgress
        [ResponseType(typeof(StudentCourseProgress))]
        public IHttpActionResult PostStudentCourseProgress(StudentCourseProgress studentcourseprogress)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.StudentCourseProgresses.Add(studentcourseprogress);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (StudentCourseProgressExists(studentcourseprogress.UserName))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = studentcourseprogress.UserName }, studentcourseprogress);
        }

        // DELETE api/StudentCourseProgress/5
        [ResponseType(typeof(StudentCourseProgress))]
        public IHttpActionResult DeleteStudentCourseProgress(string id)
        {
            StudentCourseProgress studentcourseprogress = db.StudentCourseProgresses.Find(id);
            if (studentcourseprogress == null)
            {
                return NotFound();
            }

            db.StudentCourseProgresses.Remove(studentcourseprogress);
            db.SaveChanges();

            return Ok(studentcourseprogress);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool StudentCourseProgressExists(string id)
        {
            return db.StudentCourseProgresses.Count(e => e.UserName == id) > 0;
        }
    }
}